package com.example.expresslove;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
//静态变量
	public static MediaPlayer MP3;
//	定义Textview
	public TextView text1;
	public TextView text2;
	public TextView text3;
	private int curIndex = 0;
	private Uri mUri;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

//		 播放MP3
		 MP3 = MediaPlayer.create(MainActivity.this, R.raw.ibelieve);
//		 播放位置
		 mUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.ibelieve);
		 MP3.start();  
	 
		 text1=(TextView) findViewById(R.id.id_first1);
		 text2=(TextView) findViewById(R.id.id_first2);
		 text3=(TextView) findViewById(R.id.id_first3);
//		 设置字体
		 Typeface face = Typeface.createFromAsset(getAssets(),"fonts/english.ttf"); 
		 text1.setTypeface(face);
		 text2.setTypeface(face);
		 text3.setTypeface(face);
//		 添加旋转动画
		 ImageView infoOperatingIV = (ImageView)findViewById(R.id.disk);
//		创建动画
		 Animation operatingAnim = AnimationUtils.loadAnimation(this, R.anim.xuanzhuan);  
//		匀速旋转
		 LinearInterpolator lin = new LinearInterpolator();  
		 operatingAnim.setInterpolator(lin); 
		 infoOperatingIV.startAnimation(operatingAnim); 
//		线程控制播放时间
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
//				设定时间为13秒后跳转
					Thread.sleep(13000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
//				跳转另一个activity（表白界面）
				Intent intent = new Intent();
				intent.setClass(MainActivity.this, LetterActivity.class);
				startActivity(intent);
				finish();
//				加载跳转渐变动画
				overridePendingTransition(R.anim.out_alpha,R.anim.enter_alpha);
			}
		});
//		启动线程（这里说明，线程启动方式有多种，可以参考书目的介绍）

		thread.start();


	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			//System.out.println("back PRESSED!");
			Toast.makeText(this,"宝贝别心急哦!",Toast.LENGTH_SHORT).show();
			return false;
		}else {
			return super.onKeyDown(keyCode, event);
		}
	}

/*	@Override
	protected void onPause() {
		pause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		new Handler().postDelayed(new Runnable() {
			public void run() {
				Play(curIndex,mUri);
			}
		}, 10);
		super.onResume();
	}

	private void pause() {
		if (MP3 != null && MP3.isPlaying()) {
			curIndex = MP3.getCurrentPosition();
			MP3.pause();
		}
	}

	private void Play(final int currentPosition,Uri mUri) {
		try
		{
			MP3.reset();
			MP3.setDataSource(MainActivity.this,mUri);
			//MP3.setDisplay(surfaceView.getHolder());
			MP3.setLooping(true);
			MP3.prepareAsync();
			MP3.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					MP3.seekTo(currentPosition);
					MP3.start();
				}
			});

			MP3.setOnErrorListener(new MediaPlayer.OnErrorListener() {
				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					return false;
				}
			});
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}*/

}
